const main = {
    //记录指标和分组的位置信息
    xyInfo: {
        rowType:"iden",
        columnType:"group"
    },
    //存放行列操作信息
    optionInfo: {},
    rowColInfo: {},
    setXyInfo: function(rowType, columnType){
        this.xyInfo = {
            rowType: rowType,
            columnType: columnType
        }
    },
    //初始化全局变量信息
    createTable: function (idenInfo,groupInfo) {
        // this.xyInfo = xyInfo;
        if(this.xyInfo.rowType == "iden"){
            this.optionInfo = {
                rowInfo:idenInfo,
                colInfo:groupInfo
            }
        }else {
            this.optionInfo = {
                rowInfo:groupInfo,
                colInfo:idenInfo
            }
        }
        /*if(groupInfo && groupInfo.length > 0){
          if(this.xyInfo.rowType == "iden"){
            this.optionInfo = {
              rowInfo:idenInfo,
              colInfo:groupInfo
            }
          }else {
            this.optionInfo = {
              rowInfo:groupInfo,
              colInfo:idenInfo
            }
          }
        }else {
          this.xyInfo = {
            rowType:"group",
            columnType:"iden"
          },
            this.optionInfo = {
              rowInfo:groupInfo,
              colInfo:idenInfo
            }
        }*/
        this.initTable();
    },
    //刷新表格
    refresh: function (idenInfo,groupInfo) {
        if(this.xyInfo.rowType == "iden"){
            this.optionInfo = {
                rowInfo:idenInfo,
                colInfo:groupInfo
            }
        }else {
            this.optionInfo = {
                rowInfo:groupInfo,
                colInfo:idenInfo
            }
        }
        this.initTable();
    },
    //新建表格
    initTable: function () {
        var rowInfo = this.optionInfo.rowInfo;
        var colInfo = this.optionInfo.colInfo;
        //先清空四张表的内容
        this.clearTable();
        // if(!rowInfo || !colInfo || colInfo.length === 0 || rowInfo.length === 0){
        //   /*alert("参数错误");*/
        //   return;
        // }
        // 二维表格
        if((colInfo && colInfo.length > 0)&&(rowInfo && rowInfo.length > 0)){
            //根据json树添加左侧row_table
            var maxColumn = rowTable.getDeepth_rowTable(this.deepClone(rowInfo))
            //data对象在使用的时候，会修改其属性，为不改变元数据的属性，在使用时对数据做一次深度拷贝
            var rowResult = rowTable.treeToRow({data:this.deepClone(rowInfo),maxCol:maxColumn,table:$("#row_table")});
            //上一行代码计算了左侧表格的行数，列数，以及全部递归成一纬的数组
            var crossCols = rowResult.colNum; //cross_table的列数为row_table的列数
            var dataRows = rowResult.rowNum;//data_table的行数为row_table的行数
            //根据json树构建顶部column_table
            var colResult = columnTable.treeToColumn({data:this.deepClone(colInfo),table:$("#column_table")});
            var crossRows = colResult.rowNum; //cross_table的行数为column_table的行数
            var dataCols = colResult.colNum; //data_table的列数为column_table的列数
            //根据行数列数构建交叉点表格
            crossTable.createCrossTable(crossRows,crossCols,"cross_table");
            //根据行数列数构建数据表
            var idenGroupInfo = {   //构建colnum_table和row_table时保存的行列属性信息
                rowInfo:rowResult.rowInfo,
                colInfo:colResult.colInfo
            }
            this.rowColInfo = idenGroupInfo;
            //根据行列数构建数据表
            dataTable.createDataTable(dataRows,dataCols,"data_table",this.xyInfo,idenGroupInfo);
        }else if((colInfo && colInfo.length > 0)&&(!rowInfo || rowInfo.length == 0)){//只有指标存在,将指标放入宾栏
            var colResult = columnTable.treeToColumn({data:this.deepClone(colInfo),table:$("#column_table")});
            var crossRows = colResult.rowNum; //cross_table的行数为column_table的行数
            var dataCols = colResult.colNum; //data_table的列数为column_table的列数
            dataTable.createDataTableNoGroups("data_table",dataCols,colResult.colInfo);
        }else if((rowInfo && rowInfo.length > 0)&&(!colInfo || colInfo.length == 0)){//只有指标存在,将指标放入主栏
            //根据json树添加左侧row_table
            var maxColumn = rowTable.getDeepth_rowTable(this.deepClone(rowInfo))
            var rowResult = rowTable.treeToRow({data:this.deepClone(rowInfo),maxCol:maxColumn,table:$("#row_table")});
            var crossRows = rowResult.rowNum; //cross_table的行数为row_table的行数 (与上面相反)
            dataTable.createDataTableNoGroupstwo("data_table",crossRows,rowResult.rowInfo);
        }else{
            return;
        }

    },
    //清空四张表
    clearTable: function () {
        //先清空四张表的内容
        $("#column_table tbody").html("");
        $("#row_table tbody").html("");
        $("#cross_table tbody").html("");
        $("#data_table tbody").html("");
    },
    //对象深度克隆
    deepClone: function(obj){
        var result,oClass=this.isClass(obj);
        //确定result的类型
        if(oClass==="Object"){
            result={};
        }else if(oClass==="Array"){
            result=[];
        }else{
            return obj;
        }
        for(var key in obj){
            var copy=obj[key];
            if(this.isClass(copy)=="Object"){
                result[key] = this.callee(copy); //arguments.callee(copy);//递归调用
            }else if(this.isClass(copy)=="Array"){
                result[key] = this.callee(copy);//arguments.callee(copy);
            }else{
                result[key]=obj[key];
            }
        }
        return result;
    },
    callee(data) {
        var newObj;
        newObj = JSON.parse(JSON.stringify(data));
        return newObj;
    },
    /*deepClone: function(obj) {
      var newObj = {};
      newObj = JSON.parse(JSON.stringify(obj));
      return newObj;
    },*/
    //返回传递给他的任意对象的类
    isClass: function (o){
        if(o===null) return "Null";
        if(o===undefined) return "Undefined";
        return Object.prototype.toString.call(o).slice(8,-1);
    },
//交换指标和分组的位置
    exchangeXY: function (){
        //交换指标和分组位置标志
        var row_type = (this.xyInfo.rowType == "iden") ? "group" : "iden";
        var column_type = (this.xyInfo.columnType == "iden") ? "group" : "iden";
        this.xyInfo = {
            rowType:row_type,
            columnType:column_type
        }
        //交换指标和分组信息
        var temp = {};
        temp = optionInfo.colInfo;
        optionInfo.colInfo = optionInfo.rowInfo;
        optionInfo.rowInfo = temp;
        //刷新表结构
        this.initTable();
    },
    /**
     * 读取数据
     * @returns {Array}
     */
    getData: function () {
        var datas = new Array();
        var data = {};
        $("#data_table").find("tr").each(function () {
            $(this).find("td").each(function () {
                var value = $(this).text();
                var idenCode = $(this).attr("idenCode");
                var groupCode = $(this).attr("groupCode");
                data = {
                    isRegionCode: '',
                    value:value,
                    idenCode:idenCode,
                    groupCode:groupCode
                }
                datas.push(data);
            });
        });
        return datas;
    },

    /**
     * 获取行列信息
     * @returns {{}}
     */
    getRowColInfo: function () {
        return this.rowColInfo;
    }
}

const columnTable = {
    treeToColumn: function(param){
        var result = {
            rowNum:0,  //记录指标或分组占用的行数 用于数据表的行数
            colNum:0,  //记录指标或分组占用的列数 用于交叉点表的列数
            colInfo:[] //记录分组或指标的信息[{rowIndex:,info:},{rowIndex:,info:}] 用于数据表的表格信息标识，行方向的指标或分组信息
        };
        var colInfo = new Array();
        //参数检查
        if(!param || !param["table"] || !param["data"]){
            alert('参数错误{data:a,colMax:5,table:$("#myTable")}');
            return ;
        }
        //要插入表格的树json
        var data = param["data"];
        //取得table对象
        var table = param["table"];
        //这是表格头部封装！！！！！！！
        //格式化json树,为每个节点添加上等级
        this.formatDataLevel(data);
        //格式化json树,为每个节点添加上宽度
        this.formatDataWidth(data);
        //取得树的深度，即树占用的行数
        var rowMax = 0, colMax = 0;
        colMax = this.getWidth_columnTable(data);
        //取的树的宽度，即树占用的最大列数
        rowMax = this.getDeepth_columnTable(data);
        var root = {
            level:0,
            width:colMax,
            cat_id:0,
            sub:data
        };
        //根据等级构造行
        var colIdx = 0;
        var cindex = 0; //记录实际应在单元格
        for(var rowIdx = 0; rowIdx < rowMax ; rowIdx ++){
            colIdx = 0;
            var level = rowIdx + 1;
            cindex = 0;
            var trS = getTr(root,level,rowIdx);
            //补齐后缺失单元格
            // if(cindex < colMax){
            //     var behindMiss = "";
            //     var  num = colMax - cindex;
            //     for(var j = 0 ; j < num ;j ++){
            //         behindMiss += "<td style = \'height: 22px;width: 100px;border: 1px solid black;background-color: grey\' rowidx = '"+rowIdx+"' colidx = '"+colIdx+"' cindex='"+cindex+"'></td>";
            //         colIdx ++;
            //         cindex ++;
            //     }
            //     trS = trS.replace("</tr>",behindMiss + "</tr>")
            // }
            $(trS).appendTo(table);
        }
        var tdStr = "";
        function getTr(node,level,rowIdx) {
            tdStr = "";
            getTd(node,level,rowIdx,"");
            var trStr = "<tr rowidx = \""+rowIdx+"\">" + tdStr + "</tr>";
            return trStr;
        }
        function getTd(data,level,rowIdx){
            if ( data.sub.length == 0)
            {
                return ;
            } else {
                for ( var i = 0;i < data.sub.length;i ++){

                    //记录指标和分组的层级关系path
                    if(data.attribute){
                        if(data.attribute.ig_type === "iden"){
                            //针对指标对比类型去掉多余path和pathName
                            if(data.sub[i].attribute.hasOwnProperty("Iden_type")&&data.sub[i].attribute.Iden_type==="idencomparsion"){
                                data.sub[i].attribute.path = data.attribute.path ;
                                data.sub[i].attribute.pathName = data.attribute.pathName ;
                                data.sub[i].attribute.idenCode = data.attribute.idenCode ;
                            }else{
                                data.sub[i].attribute.path = data.attribute.path + "-" + data.sub[i].attribute.idenCode;
                                data.sub[i].attribute.pathName = data.attribute.pathName + "-" + data.sub[i].attribute.idenName;
                            }

                        }else if(data.attribute.ig_type === "group"){
                            var parPath = data.attribute.path + "-";
                            var parPathName = data.attribute.pathName + "-";
                            if(data.attribute.isPeriod == true){
                                parPath = "";
                                parPathName = "";
                            }
                            data.sub[i].attribute.path = parPath+ data.sub[i].attribute.groupCode;
                            data.sub[i].attribute.pathName = parPathName + data.sub[i].attribute.groupName;
                        }
                    }else{
                        if(data.sub[i].attribute.ig_type === "iden"){
                            data.sub[i].attribute.path =  data.sub[i].attribute.idenCode;
                            data.sub[i].attribute.pathName =  data.sub[i].attribute.idenName;
                        }else if(data.sub[i].attribute.ig_type === "group"){
                            data.sub[i].attribute.path =  data.sub[i].attribute.groupCode;
                            data.sub[i].attribute.pathName =  data.sub[i].attribute.groupName;
                        }
                    }

                    //加上报告期相关信息
                    if(data.attribute){
                        if(data.attribute.isPeriod != undefined && data.attribute.isPeriod !=null && data.attribute.isPeriod === true){
                            var periods = data.attribute.groupCode;
                            if(data.sub[i].attribute.isPeriod != undefined && data.sub[i].attribute.isPeriod != null && data.sub[i].attribute.isPeriod === false){
                                data.sub[i].attribute.period = periods;
                            }
                        }else if(data.attribute.isPeriod != undefined && data.attribute.isPeriod !=null && data.attribute.isPeriod === false){
                            var periods = data.attribute.period;
                            data.sub[i].attribute.period = periods;
                        }
                    }

                    if(data.sub[i].level == level){
                        var rowspanStr = "";
                        if(data.sub[i].sub.length == 0){
                            if(data.sub[i].level < rowMax){
                                rowspanStr += " rowspan = '" + (rowMax - data.sub[i].level+1)+"'";
                            }
                        }
                        tdStr += "<td style='width: 100px;height: 35px;' colspan='"+data.sub[i].width+"' rowidx = '"+rowIdx+"' colidx = '"+colIdx+"' cindex='"+cindex+"' "+rowspanStr+" title='" + data.sub[i].name + "'><span>" + data.sub[i].name + "</span></td>";
                        data.sub[i].cindex = cindex;
                        colIdx ++;
                        cindex += data.sub[i].width;
                    }
                    if(data.sub[i].sub.length == 0){
                        var attrinfo = data.sub[i].attribute;
                        // if(attrinfo.idenCode == ""){//针对指标对比类型取指标idenCode
                        //   attrinfo.idenCode = data.attribute.idenCode;
                        // }
                        var info = {
                            index:colIdx,
                            info:attrinfo
                        };
                        colInfo.push(info);
                    }
                    getTd(data.sub[i],level,rowIdx) ;
                }
            }
        }
        //构建返回对象
        result.rowNum = rowMax;
        result.colNum = colMax;
        result.colInfo = colInfo;
        return result;
    },
    //格式化数据，为各节点添加上等级
    formatDataLevel: function (data) {
        //获得json树的深度
        var ans = 0;
        var getColDeepth = function (data) {
            //计算出各指标树或分组树的最大值
            var maxRow = 0;
            for(var ddi = 0;ddi < data.length ;ddi ++){
                dfs(data[ddi],1);
                maxRow = Math.max(ans,maxRow);
            }
            return maxRow;
        }
        //递归计算树的深度
        var dfs = function(data ,deep){
            if ( data.sub.length == 0 )
            {
                ans = Math.max(ans , deep ) ;
                data.level = deep;
                return ;
            }
            else {
                for ( var i = 0;i < data.sub.length;i ++){
                    dfs(data.sub[i] , deep + 1) ;
                }
                data.level = deep;
            }
        }
        getColDeepth(data);
    },
    //格式化数据为每个节点添加上节点的宽度
    formatDataWidth: function (data) {
        //递归计算每个节点的宽度
        var widthN = 0;
        var  getWidthNum = function (node){
            if ( node.sub.length == 0 )
            {
                widthN ++;
                return ;
            } else {
                for (var i = 0;i < node.sub.length;i ++){
                    getWidthNum(node.sub[i]) ;
                }
            }
        }
        //递归每个节点
        var  recursiveData = function (node){
            if ( node.sub.length == 0 )
            {
                node.width = 0;
                return ;
            } else {
                for (var i = 0;i < node.sub.length;i ++){
                    recursiveData(node.sub[i]);
                    getWidthNum(node.sub[i]) ;
                    node.sub[i].width = widthN;
                    widthN = 0;
                }
            }
        }

        for(var i = 0 ; i < data.length ; i ++){
            widthN = 0;
            recursiveData(data[i]);
            getWidthNum(data[i]);
            data[i].width = widthN;
        }
    },
    //取得json树到占用的行数
    getDeepth_columnTable: function (data) {
        var maxRow = 0;
        var ans = 0;
        //递归计算树的深度
        var dfs = function(data ,deep){
            if ( data.sub.length == 0 )
            {
                ans = Math.max(ans , deep ) ;
                data.level = deep;
                return ;
            }
            else {
                for ( var i = 0;i < data.sub.length;i ++)
                    dfs(data.sub[i] , deep + 1) ;
                data.level = deep;
            }
        }
        for(var i = 0; i < data.length ; i ++){
            dfs(data[i],1);
            maxRow = Math.max(ans,maxRow);
        }
        return maxRow;
    },
    //取的json树总共占用的列数
    getWidth_columnTable: function (data) {
        var value = 0;
        //递归计算宽度
        var  getWidthNum = function (node){
            if ( node.sub.length == 0 )
            {
                value ++;
                return ;
            } else {
                for (var i = 0;i < node.sub.length;i ++){
                    getWidthNum(node.sub[i]) ;
                }
            }
        }
        for(var i = 0 ; i < data.length ;i ++){
            getWidthNum(data[i]);
        }
        return value;
    }
}

const crossTable = {
    createCrossTable: function (rownum,colnum,tableId) {
        var crossTable = $("#"+tableId);
        for(var i = 0 ; i < rownum ; i ++){
            var trStr = "<tr>"
            var tdStr = "";
            for(var j = 0; j < colnum ; j ++){
                tdStr += "<td style='width: 100px;'></td>";
            }
            trStr += tdStr;
            trStr += "</tr>";
            $(trStr).appendTo(crossTable);
        }
        // var trStr = "<tr rowspan='"+ rowmun + "'>"
        // trStr+="<td style='width: 100px;' colspan='"+ colnum + "'></td>"
        // trStr += "</tr>";
        // crossTable.html(trStr)
    }
}

const dataTable = {
    //指标和分组都存在是调用
    createDataTable: function (rownum,colnum,tableId,xyInfo,optionInfo) {
        //取得指标和分组信息
        var idenGroupInfo = this.getIdenInfoGroupInfo(xyInfo,optionInfo);
        var idenInfo = idenGroupInfo.idenInfo;
        var groupInfo = idenGroupInfo.groupInfo;
        //根据指定的行数列数新建table
        var crossTable = $("#"+tableId);
        var iden = idenInfo[i];
        var group = groupInfo[j];
        for(var i = 0 ; i < rownum ; i ++){
            var trStr = "<tr >"
            var tdStr = "";
            for(var j = 0; j < colnum ; j ++){
                //取得cell对应的指标和分组信息
                if(xyInfo.rowType == "iden"){
                    iden = idenInfo[i];
                    group = groupInfo[j];
                }else {
                    iden = idenInfo[j];
                    group = groupInfo[i];
                }
                var period =  (group.info.period != undefined && group.info.period != null && group.info.period != "") ? group.info.period :"";
                var groupPath = "";
                var groupPathName = "";
                var groupCode = "";
                //如果最后一层为报告期period为“”
                if(!group.info.isPeriod){
                    groupCode = group.info.groupCode;
                    groupPath = (group.info.path != undefined && group.info.path != null && group.info.path != "") ? group.info.path :"";
                    groupPathName = (group.info.pathName != undefined && group.info.pathName != null && group.info.pathName != "") ? group.info.pathName :"";
                }
                var idenPath = (iden.info.path != undefined && iden.info.path != null && iden.info.path != "") ? iden.info.path :"";
                var idenPathName = (iden.info.pathName != undefined && iden.info.pathName != null && iden.info.pathName != "") ? iden.info.pathName :"";

                tdStr += "<td style='width: 100px;height: 35px;' rowidx='"+i+"' colidx='"+j+"' idenCode='"+iden.info.idenCode+"'idencomparsion='"+iden.info.idencomparsion +"' idenPath='"+ idenPath +"' idenPathName='" + idenPathName +"' period='" + period
                    + "' groupCode='"+groupCode+"' groupPath='"+groupPath + "' groupPathName='" + groupPathName+"' isRegionCode='" + group.info.isRegionCode
                    + "'><input type='text' value='' placeholder='-'/><span style='display: none;'></span></td>";
            }
            trStr += tdStr;
            trStr += "</tr>";
            $(trStr).appendTo(crossTable);
        }
    },
    //分组不存在时候调用
    createDataTableNoGroups: function (tableId,cloumn,idneInfo) {
        //根据指定的行数列数新建table
        var crossTable = $("#"+tableId);
        var trStr = "<tr >"
        var tdStr = "";
        var iden;
        for(var j = 0; j < cloumn ; j ++){
            //取得cell对应的指标和分组信息
            iden = idneInfo[j];
            tdStr += "<td width='100' rindex='0' cindex='"+j+"' idenCode='"+iden.info.idenCode+"' groupCode='' isRegionCode=''><input type='text' placeholder='-' v-model='filter.region'/><span style='display: none;'></span></td>";
        }
        trStr += tdStr;
        trStr += "</tr>";
        $(trStr).appendTo(crossTable);
    },
    //没有分组，指标放入主栏时使用
    createDataTableNoGroupstwo: function (tableId,cloumn,idneInfo) {
        //根据指定的行数列数新建table
        var crossTable = $("#"+tableId);
        var trbox = '';
        for(var j = 0; j < cloumn ; j ++){
            var trStr = "<tr >"
            var tdStr = "";
            //取得cell对应的指标和分组信息
            var iden = idneInfo[j];
            tdStr += "<td width='100' rindex='0' cindex='"+j+"' idenCode='"+iden.info.idenCode+"' groupCode='' isRegionCode=''><input type='text' placeholder='-' v-model='filter.region'/><span style='display: none;'></span></td>";

            trStr += tdStr;
            trStr += "</tr>";
            trbox += trStr;
        }
        $(trbox).appendTo(crossTable);
    },
    //根据位置关系获取指标数据和分组数据
    getIdenInfoGroupInfo: function (xyInfo,optionInfo) {
        var idenGroupInfo = {
            idenInfo:null,
            groupInfo:null
        };
        if(xyInfo.rowType == "iden"){
            idenGroupInfo.idenInfo = optionInfo.rowInfo;
            idenGroupInfo.groupInfo = optionInfo.colInfo;
        }else {
            idenGroupInfo.idenInfo = optionInfo.colInfo;
            idenGroupInfo.groupInfo = optionInfo.rowInfo;
        }
        return idenGroupInfo;
    }
}

const rowTable = {
    treeToRow: function(param){
        var result = {
            rowNum:0,  //记录指标或分组占用的行数 用于数据表的行数
            colNum:0,  //记录指标或分组占用的列数 用于交叉点表的列数
            rowInfo:[] //记录分组或指标的信息[{rowIndex:,info:},{rowIndex:,info:}] 用于数据表的表格信息标识，行方向的指标或分组信息
        };
        var rowInfo = new Array();
        var colList = new Array();
        var childNodeColArray = new Array();
        var totalSize = 0; //要插入的树的最大宽度
        //参数检查
        if(!param || !param["table"] || !param["data"]){
            alert('参数错误{data:a,colMax:5,table:$("#myTable")}');
            return ;
        }
        //要插入表格的树json
        var data = param["data"];
        //格式化数据添为每个节点加上
        this.formatDataLevel(data);
        //列数 各指标树或分组树的最大深度
        var colMax = 0;
        if(param && param["maxCol"] > 0){
            colMax = param["maxCol"];
        }
        //取得table对象
        var table = param["table"];
        //取得指标树或分组树的宽度
        $.each( data,function(n, value){
            totalSize += visitSubNode(value,0,colList,colMax,"");
        });
        //构造添加的行
        for(var rowIdx =0;rowIdx < totalSize; rowIdx++){
            var str = "<tr>";
            str += getTd(colList,rowIdx);
            str +="</tr>";
            $(str).appendTo(table);
        }
        //取得table td
        function getTd(colList,rowIdx){

            var str = '';
            var columnIdenx = 0;
            for(var i =0;i<colList.length;i++){
                var colArray = colList[i];
                if(colArray != null && colList[i][rowIdx]!= null){
                    if(!colList[i][rowIdx].value){
                        continue;
                    }
                    var colspanStr = "";
                    if(colList[i][rowIdx].value.level < colMax && colList[i][rowIdx].value.sub.length < 1){
                        colspanStr += " colspan = \"" + (colMax - colList[i][rowIdx].value.level +1)+"\"";
                    }
                    str += "<td style='width: 100px;height: 35px;' rowidx=\""+colList[i][rowIdx].rowIdx+"\"";
                    var rowspan = colList[i][rowIdx].size;
                    if(rowspan >1){
                        str += " rowspan= \""+rowspan + "\"";
                    }
                    str += colspanStr;
                    str += " colidx = \"" + columnIdenx + "\"";
                    columnIdenx ++;
                    var text = "";
                    if(colList[i][rowIdx].value){
                        text = colList[i][rowIdx].value.name;
                    }
                    str += " title = \"" + text + "\"";
                    str += "\"><span>"+text+"</span></td>";///*"  prowidx=\""+ colList[i][rowIdx].pRowIdx +*/

                    if(colList[i][rowIdx].value && colList[i][rowIdx].value.sub < 1){
                        var attrinfo = colList[i][rowIdx].value.attribute;
                        // if(attrinfo.idenCode == ""){//针对指标对比类型取指标idenCode
                        //   attrinfo.idenCode = colList[i-1][0].value.attribute.idenCode;
                        // }
                        var rinfo = {
                            index:rowIdx,
                            info:attrinfo
                        };
                        rowInfo.push(rinfo);
                    }
                }
            }
            return str;
            //////计算每行第一列！！！！！！！！！！
        }
        //添加节点
        function addNewNode(colList,colIdx,size,value,childCount){
            var colArray = colList[colIdx];
            if(colArray == null){
                colArray = new Array();
                colList[colIdx] = colArray;
            }
            var newData = {
                rowIdx: 0,
                pRowIdx: 0,
                colIdx: colIdx,
                size:size,
                value: value,
                toString:function(){
                    return "{rowIdx:"+ this.rowIdx+",pRowIdx:"+ this.pRowIdx+ ",colIdx:"+ this.colIdx+ ",size:"+ this.size +",value:"+ this.value.cat_id + "}";
                }
            };
            var old = colArray.pop();
            if(old != null){
                newData.rowIdx = (old.rowIdx + old.size);
                colArray[old.rowIdx] = old;
            }

            colArray[newData.rowIdx] = newData;

            if(childCount >0){
                var childColList = colList[colIdx +1];
                var tempArray = new Array();
                for(var i=0;i< childCount;i++){
                    var temp = childColList.pop();
                    if(temp){
                        temp.pRowIdx = newData.rowIdx;
                        tempArray.push(temp);
                    }
                }
                for(var i=0;i< childCount;i++){
                    var temp = tempArray.pop();
                    if(temp){
                        childColList[temp.rowIdx] = temp;
                    }
                }
            }
            return newData.rowIdx;
        }
        //获得表的宽度，占用的行数
        function visitSubNode(node,colIndex,colList,colMax,periods){
            if(colIndex < colMax -1 && node.sub && node.sub.length >0){
                var _tempPeriod = periods;
                var sub = node.sub;
                var total = 0;
                var childCount = 0;

                //支持报告期下带到所有下级分组节点
                if(node.attribute){
                    if(node.attribute.isPeriod && node.attribute.isPeriod === true){
                        periods = node.attribute.groupCode;
                    }
                }


                //为根节点添加path
                if(node.attribute){
                    //报告期层不加到path
                    if(node.attribute.path === ""){
                        if(node.attribute.ig_type === "iden"){
                            node.attribute.path = node.attribute.idenCode;
                            node.attribute.pathName = node.attribute.idenName;
                        }else if(node.attribute.ig_type === "group"){
                            if(node.attribute.isPeriod === false){
                                node.attribute.path = node.attribute.groupCode;
                                node.attribute.pathName = node.attribute.groupName;
                            }
                        }
                    }
                }


                $.each( sub, function(n, value){
                    //为下级节点添加path
                    if(node.attribute){
                        if(node.attribute.ig_type === "iden"){
                            if(node.attribute.isPeriod === false){
                                if(value.attribute.hasOwnProperty("Iden_type")&&value.attribute.Iden_type ==="idencomparsion"){
                                    value.attribute.path = node.attribute.path;
                                    value.attribute.pathName = node.attribute.pathName;
                                    value.attribute.idenCode = node.attribute.idenCode;
                                }else{
                                    value.attribute.path = node.attribute.path + "-" + value.attribute.idenCode;
                                    value.attribute.pathName = node.attribute.pathName + "-" + value.attribute.idenName;
                                }
                            }

                        }else if(node.attribute.ig_type === "group"){
                            if(node.attribute.isPeriod === false){
                                value.attribute.path = node.attribute.path + "-" + value.attribute.groupCode;
                                value.attribute.pathName = node.attribute.pathName + "-" + value.attribute.groupName;
                            }
                        }


                    }else{
                        if(value.attribute.ig_type === "iden"){
                            value.attribute.path = value.attribute.idenCode;
                        }else if(value.attribute.ig_type === "group"){
                            if(node.attribute.isPeriod === false){
                                value.attribute.path = value.attribute.groupCode;
                            }
                        }
                    }


                    var i = visitSubNode(value, colIndex+1,colList,colMax,periods);
                    total += i;
                    childCount++;
                });
                //支持报告期下带到所有下级分组节点
                if(_tempPeriod && _tempPeriod != ""){
                    if(node.attribute){
                        if(node.attribute.isPeriod != undefined &&  node.attribute.isPeriod != null&& node.attribute.isPeriod === false){
                            node.attribute.period = _tempPeriod;
                        }
                    }
                }
                addNewNode(colList,colIndex,total,node,childCount)
                return total;
            }else{
                //支持报告期下带到所有下级分组节点
                var _tempPeriod = periods;
                if(node.attribute){
                    if(node.attribute.isPeriod && node.attribute.isPeriod === true){
                        periods = node.attribute.groupCode;
                    }
                }

                //为根节点添加path
                if(node.attribute){
                    //报告期层不加到path
                    if(node.attribute.path === ""){
                        if(node.attribute.ig_type === "iden"){
                            node.attribute.path = node.attribute.idenCode;
                            node.attribute.pathName = node.attribute.idenName;
                        }else if(node.attribute.ig_type === "group"){
                            if(node.attribute.isPeriod === false){
                                node.attribute.path = node.attribute.groupCode;
                                node.attribute.pathName = node.attribute.groupName;
                            }
                        }
                    }
                }

                var value = null;
                if(!node.isNull){
                    value = node;
                }
                if(colIndex < colMax -1){
                    node.isNull = true;
                    var  count = visitSubNode(node, colIndex+1,colList,colMax,periods);
                    //支持报告期下带到所有下级分组节点
                    if(_tempPeriod && _tempPeriod != ""){
                        if(node.attribute){
                            if(node.attribute.isPeriod != undefined &&  node.attribute.isPeriod != null&& node.attribute.isPeriod === false){
                                node.attribute.period = _tempPeriod;
                            }
                        }
                    }
                    addNewNode(colList,colIndex,1,value,1);
                    return count;
                }
                if(_tempPeriod && _tempPeriod != ""){
                    if(node.attribute){
                        if(node.attribute.isPeriod != undefined &&  node.attribute.isPeriod != null&& node.attribute.isPeriod === false){
                            node.attribute.period = _tempPeriod;
                        }
                    }
                }
                addNewNode(colList,colIndex,1,value,0);
                node.colIndex = colIndex;
                childNodeColArray.push(node);
                return 1;
            }
        }
        //返回建表信息
        result.rowNum = totalSize;
        result.colNum = colMax;
        result.rowInfo = rowInfo;
        return result;
    },
//获得json树的深度
    getDeepth_rowTable: function (data) {
        //计算出各指标树或分组树的最大值
        var maxColumn = 0;
        var ans = 0;
        for(var ddi = 0;ddi < data.length ;ddi ++){
            dfs(data[ddi],1);
            maxColumn = Math.max(ans,maxColumn);
        }
        //递归计算树的深度
        function dfs(data ,deep){
            if ( data.sub.length == 0 )
            {
                ans = Math.max(ans , deep ) ;
                return ;
            }
            else {
                for ( var i = 0;i < data.sub.length;i ++)
                    dfs(data.sub[i] , deep + 1) ;
            }
        }
        return maxColumn;
    },
//格式化数据，为各节点添加上等级
    formatDataLevel: function (data) {
        //获得json树的深度
        var temp = 0;
        var getColDeepth = function (data) {
            //计算出各指标树或分组树的最大值
            var maxRow = 0;
            for(var ddi = 0;ddi < data.length ;ddi ++){
                dfs(data[ddi],1);
                maxRow = Math.max(temp,maxRow);
            }
            return maxRow;
        }
        //递归计算树的深度
        var dfs = function(data ,deep){
            if ( data.sub.length == 0 )
            {
                temp = Math.max(temp , deep ) ;
                data.level = deep;
                return ;
            }
            else {
                for ( var i = 0;i < data.sub.length;i ++){
                    dfs(data.sub[i] , deep + 1) ;
                }
                data.level = deep;
            }
        }
        getColDeepth(data);
    }
}